clear all
set more off, permanently


global replica_dir="<add path to your working directory>"
global data_dir="$replica_dir/data"
global output_dir="$replica_dir/output"

global simce_data_dir="<add path to your SIMCE data directory>"
*------------------------------------------------------------------------------*

* SIMCE 2009 4b

*------------------------------------------------------------------------------*

**# Student scores

/*

Note:
-----
mrun: 		unique student identifier to link data across years	
idalumno: 	unique identifier for linkage within year, e.g. parents, teachers surveys
	
------------------------------------------------------------------------------*/

cd "$data_dir" 
use "$simce_data_dir/4B/simce4b2009/Archivos DTA (Stata)/simce4b2009_alu_publica_final.dta", clear

keep if noptje_lect4b_alu==0 | noptje_mate4b_alu==0 | noptje_nat4b_alu==0 

duplicates report mrun
duplicates examples mrun
duplicates tag mrun, g(ndupobs)
unique mrun

keep if ndupobs==0

rename  (ptje_lect4b_alu 	ptje_mate4b_alu 	ptje_nat4b_alu ) /// 
		(lect_pre			mate_pre			nat_pre)


keep idalumno mrun rbd lect_pre mate_pre nat_pre //noptje_mate4b_alu noptje_lect4b_alu noptje_nat4b_alu

save "$data_dir/simce_b4_2009.dta", replace


*------------------------------------------------------------------------------*

**#  Parental survey

*------------------------------------------------------------------------------*

use "$simce_data_dir/4B/simce4b2009/Archivos DTA (Stata)/simce4b2009_cpad_publica_final.dta", clear

foreach var of varlist cpad_*{
replace `var'=. if `var'==0 | `var'==99
}


gen dad_edu_pre=.
gen mom_edu_pre=.

forvalues x=1/9 {
	
replace dad_edu_pre=`x' if cpad_p09_01_0`x'==1
replace mom_edu_pre=`x' if cpad_p09_02_0`x'==1

}
forvalues x=10/20 {
	
replace dad_edu_pre=`x' if cpad_p09_01_`x'==1
replace mom_edu_pre=`x' if cpad_p09_02_`x'==1
}


gen pad_inc_pre=.
forvalues x=1/9 {
replace pad_inc_pre=`x' if cpad_p10_0`x'==1
}
forvalues x=10/15 {
replace pad_inc_pre=`x' if cpad_p10_`x'==1
}


keep idalumno dad_edu_pre mom_edu_pre pad_inc_pre


merge 1:1 idalumno using "$data_dir/simce_b4_2009.dta" 
drop _merge

sort mrun

save "$data_dir/simce_b4_2009.dta", replace



*------------------------------------------------------------------------------*

**#  School survey

*------------------------------------------------------------------------------*

use "$simce_data_dir/4B/simce4b2009/Archivos DTA (Stata)/simce4b2009_rbd_publica_final.dta", clear

egen sch_stno_pre = rowmax(nalu_4b_rbd nalu_lect4b_rbd nalu_mate4b_rbd nalu_nat4b_rbd)

rename 	(cod_depe1		cod_grupo 		cod_reg_rbd) ///
		(sch_depe1_pre	sch_gse_pre 	cod_reg_rbd_pre)

keep  rbd sch_stno_pre sch_depe1_pre sch_gse_pre cod_reg_rbd_pre

merge 1:m rbd using "$data_dir/simce_b4_2009.dta"

//browse if _merge==1
keep if _merge==3
drop _merge


drop idalumno
rename rbd rbd_pre
sort mrun

save "$data_dir/simce_b4_2009.dta", replace

*------------------------------------------------------------------------------*





 
